package com.example.tests1;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.openqa.selenium.support.ui.ExpectedConditions.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import ru.esteru.selenium.factory.WebDriverFactory;

public class changeEmail {
  private WebDriver driver;
  private WebDriverWait wait;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
	  driver = WebDriverFactory.getDriver(DesiredCapabilities.firefox());
        wait = new WebDriverWait(driver, 10);
    baseUrl = "http://localhost/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testUntitled2() throws Exception {
    goToMainPage();
    loginAs("admin", "admin");
    goToProfilePage();
    changeUserEmail("admin@admin.net");
    assertFalse(ErrorMessege());
    logOut();
  }

private boolean ErrorMessege() {
		return driver.findElements(By.className("error")).size() > 0;
}

private void logOut() {
	wait.until(visibilityOfElementLocated(By.linkText("Log out"))).click();
	assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to log out[\\s\\S]$"));
}

private void changeUserEmail(String email) {
	WebElement changeEmail = wait.until(visibilityOfElementLocated(By.id("email")));
	changeEmail.clear();
    changeEmail.sendKeys(email);
    wait.until(visibilityOfElementLocated(By.name("submit"))).click();
}

private void goToProfilePage() {
	wait.until(visibilityOfElementLocated(By.linkText("My profile"))).click();
}

private void loginAs(String username, String password) {
	WebElement userName = wait.until(visibilityOfElementLocated(By.id("username")));
	userName.clear();
    userName.sendKeys(username);
    WebElement passWord = wait.until(visibilityOfElementLocated(By.name("password")));
	passWord.clear();
    passWord.sendKeys(password);
    wait.until(visibilityOfElementLocated(By.name("submit"))).click();
}

private void goToMainPage() {
	driver.get(baseUrl + "/php4dvd/");
}

  @After
  public void tearDown() throws Exception {
   
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alert.getText();
    } finally {
      acceptNextAlert = true;
    }
  }
}
